var p = 0, c = 0, d = 0
var api = require('../../utils/api.js')
var area = require('../../utils/area.js')
Page({
    data: {
        provinceName:[],
        provinceCode: [],
        provinceSelIndex: '',

        cityName: [],
        cityCode: [],
        citySelIndex: '',

        districtName: [],
        districtCode: [],
        districtSelIndex: '',

        countryEnabled: true,
        cityEnabled: false,
        districtEnabled: false,

        stages: [],
        stageSelIndex: '',
        station: [],
        stationSelIndex: ''
    },
    onLoad:function(options){
        //this.setAreaData();
        this.setAddress();
    },
    changeStation: function(e) {
        var index = e.detail.value;
        var currentId = this.data.station[index].id; // 这个id就是选中项的id
        this.setData({
            stationSelIndex: e.detail.value
        })
    },
    changeStage: function(e) {
        var index = e.detail.value;
        var currentId = this.data.stages[index].id; // 这个id就是选中项的id
        this.setData({
            stageSelIndex: e.detail.value
        })
    },
    // 选择省
    changeProvince: function(e) {
        this.resetAreaData('province')
        p = e.detail.value
        this.setAreaData('province', p)
    },
    // 选择市
    changeCity: function(e) {
        this.resetAreaData()
        c = e.detail.value
        this.setAreaData('city', p, c)
    },
    // 选择区
    changeDistrict: function(e) {
        d = e.detail.value
        this.setAreaData('district', p, c, d)
    },
    setAddress: function(e) {
        api.post({
            url: 'wxapp/public/getStation',
            data: {'ajax':1},
            success: res => {
                if (res.code == 1) {
                    this.setData({ station: res.data })
                }
            }
        });

        api.post({
            url: 'wxapp/public/getStage',
            data: {'ajax':1},
            success: res => {
                if (res.code == 1) {
                    this.setData({ stages: res.data })
                }
            }
        });
    },
    setAreaData: function(t, p, c, d){
        switch (t) {
            case 'province':
                this.setData({
                    provinceSelIndex: p,
                    cityEnabled: true
            })
            break;
            case 'city':
                this.setData({
                citySelIndex: c,
                districtEnabled: true
            })
            break;
            case 'district':
                this.setData({
                districtSelIndex: d
            })
            break;
        }
    
        var p = p || 0 // provinceSelIndex
        var c = c || 0 // citySelIndex
        var d = d || 0 // districtSelIndex
        // 设置省的数据
        var province = area['0']
        var provinceName = [];
        var provinceCode = [];
        for (var item in province) {
            provinceName.push(province[item])
            provinceCode.push(item)
        }
        this.setData({
            provinceName: provinceName,
            provinceCode: provinceCode
        })
        // 设置市的数据
        var city = area[provinceCode[p]]
        var cityName = [];
        var cityCode = [];
        for (var item in city) {
            cityName.push(city[item])
            cityCode.push(item)
        }
        this.setData({
            cityName: cityName,
            cityCode: cityCode
        })
        // 设置区的数据
        var district = area[cityCode[c]]
        var districtName = [];
        var districtCode = [];
        for (var item in district) {
            districtName.push(district[item])
            districtCode.push(item)
        }
        this.setData({
            districtName: districtName,
            districtCode: districtCode
        })
    },
    // 重置数据
    resetAreaData: function(type) {
        this.setData({
            districtName: [],
            districtCode: [],
            districtSelIndex: '',
            districtEnabled: false
        })
        if (type == 'province') {
            this.setData({
                cityName: [],
                cityCode: [],
                citySelIndex: ''
            })
        }
    },
    formSubmit(e){
        var detail = e.detail;
        api.post({
            url: 'user/address/update',
            data: {form_id: detail.formId},
            success: res => {
                api.post({
                    url: 'user/address/edit',
                    data: detail.value,
                    success: data => {
                        if (data.code == 1) {
                            wx.showToast({
                                title: data.msg,
                                icon: 'success',
                                duration: 800
                            })
                            setTimeout(function(){ wx.switchTab({ url: '/pages/my/address'}); }, 800);
                        }
                        if (data.code == 0) {
                            wx.showModal({
                                content: data.msg,
                                showCancel: false,
                                success: function (res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    }
                                }
                            })
                        }
                    }
                });
            }
        });

    }
});